/**
 * 
 */
package com.es.disped.service.sms;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;

import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.es.disped.api.sms.SmsService;
import com.es.disped.common.model.MsgModel;
import com.es.disped.common.sms.SmsClient;

/**
 * @author Anson<br>
 *
 * Copyright by EasyShare 2019<br>
 *
 * All right reserved<br>
 *
 * Created on 下午3:10:49<br>
 *
 * 名称：SmsServiceImpl.java<br>
 *
 * 描述：<br>
 */
@Service("smsService")
public class SmsServiceImpl implements SmsService{
	
	@Autowired
	SmsClient smsClient;
	
	/* (non-Javadoc)
	 * @see com.es.disped.api.sms.SmsService#sendMessage(java.lang.String, java.lang.String)
	 */
	@Override
	public MsgModel sendMessage(String signName, String phone, String templateCode, String paramJson) {
		smsClient.getRequest().setMethod(MethodType.POST);
		smsClient.getRequest().setDomain("dysmsapi.aliyuncs.com");
		smsClient.getRequest().setVersion("2017-05-25");
		smsClient.getRequest().setAction("SendSms");
		smsClient.getRequest().putQueryParameter("RegionId", SmsClient.ALI_MESSAGE_REGIION_ID);
		smsClient.getRequest().putQueryParameter("SignName", signName);
		smsClient.getRequest().putQueryParameter("PhoneNumbers", phone);
		smsClient.getRequest().putQueryParameter("TemplateCode", templateCode);
		//可选参数
		if(paramJson!=null)
		{
			smsClient.getRequest().putQueryParameter("TemplateParam", paramJson);
		}
        try {
        	smsClient.setResponse(smsClient.getClient().getCommonResponse(smsClient.getRequest()));
		} catch (ClientException e) {
			return new MsgModel(HttpStatus.EXPECTATION_FAILED.value(),"短信接口调用失败");
		}
        
        System.out.println(">>>>>>>>>>>>>>>>>>>>>>"+smsClient.getResponse().getData());
        System.out.println(">>>>>>>>>>>>>>>>>>>>>>"+smsClient.getResponse().getHttpStatus());
        
		return new MsgModel(smsClient.getStatus(), smsClient.getAliMsg().getMessage());
	}

	/* (non-Javadoc)
	 * @see com.es.disped.api.sms.SmsService#sendMessages(java.lang.String, java.lang.String)
	 */
	@Override
	public MsgModel sendMessages(String signNameJsonArray, String phoneJsonArray,
			String templateCode, String paramJsonArray) {
		smsClient.getRequest().setMethod(MethodType.POST);
		smsClient.getRequest().setDomain("dysmsapi.aliyuncs.com");
		smsClient.getRequest().setVersion("2017-05-25");
		smsClient.getRequest().setAction("SendBatchSms");
		smsClient.getRequest().putQueryParameter("RegionId", SmsClient.ALI_MESSAGE_REGIION_ID);
		smsClient.getRequest().putQueryParameter("SignNameJson", signNameJsonArray);
		smsClient.getRequest().putQueryParameter("PhoneNumberJson", phoneJsonArray);
		smsClient.getRequest().putQueryParameter("TemplateCode", templateCode);
		smsClient.getRequest().putQueryParameter("TemplateParamJson", paramJsonArray);
        try {
        	smsClient.setResponse(smsClient.getClient().getCommonResponse(smsClient.getRequest()));
		} catch (ClientException e) {
			return new MsgModel(HttpStatus.EXPECTATION_FAILED.value(), "短信接口调用失败");
		}
        
        System.out.println(">>>>>>>>>>>>>>>>>>>>>>"+smsClient.getResponse().getData());
        System.out.println(">>>>>>>>>>>>>>>>>>>>>>"+smsClient.getResponse().getHttpStatus());
        
		return new MsgModel(smsClient.getStatus(), smsClient.getAliMsg().getMessage());
	}

	/* (non-Javadoc)
	 * @see com.es.disped.api.sms.SmsService#queryMessage(java.lang.String, java.lang.String, java.lang.Long, java.lang.Long)
	 */
	@Override
	public MsgModel queryMessage(String phoneNumber, String sendDate, Long currentPage, Long pageSize) {
		smsClient.getRequest().setMethod(MethodType.POST);
		smsClient.getRequest().setDomain("dysmsapi.aliyuncs.com");
		smsClient.getRequest().setVersion("2017-05-25");
		smsClient.getRequest().setAction("QuerySendDetails");
		smsClient.getRequest().putQueryParameter("RegionId", SmsClient.ALI_MESSAGE_REGIION_ID);
		smsClient.getRequest().putQueryParameter("PhoneNumber", phoneNumber);
		smsClient.getRequest().putQueryParameter("SendDate", sendDate);
		smsClient.getRequest().putQueryParameter("CurrentPage", String.valueOf(currentPage));
		smsClient.getRequest().putQueryParameter("PageSize", String.valueOf(pageSize));
        try {
        	smsClient.setResponse(smsClient.getClient().getCommonResponse(smsClient.getRequest()));
		} catch (ClientException e) {
			return new MsgModel(HttpStatus.EXPECTATION_FAILED.value(), "短信接口调用失败");
		}
        
        System.out.println(">>>>>>>>>>>>>>>>>>>>>>"+smsClient.getResponse().getData());
        System.out.println(">>>>>>>>>>>>>>>>>>>>>>"+smsClient.getResponse().getHttpStatus());
        
		return new MsgModel(smsClient.getStatus(), smsClient.getAliMsg().getMessage(),smsClient.getAliMsg());
	}
}
